package me.zk.practice1.hw;

import java.util.*;

public class HWBJJ30 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String[] s = scanner.nextLine().split(" ");
            if (map.containsKey(s[1])) {
                map.get(s[1]).add(s[0]);
            } else {
                List<String> list = new ArrayList<>();
                list.add(s[0]);
                map.put(s[1],list);
            }
        }
        String b = scanner.nextLine();
        if (map.containsKey(b)) {
            List<String> res = new ArrayList<>();
            get(map,res,b);
            for (String s : res) {
                System.out.println(s);
            }
        }

    }

    public static  void  get(Map<String, List<String>> map, List<String> list,String key) {
        if (map.containsKey(key)) {
            for (String s : map.get(key)) {
                list.add(s);
                get(map,list,s);
            }
        }

    }
}
